package com.future.controller;

import javax.validation.constraints.NotNull;

import com.future.common.annotation.ControllerMapping;
import com.future.common.base.BaseController;
import com.future.dao.DiseaseDao;
import com.future.model.Disease;
import com.future.requestbean.DiseaseAdd;
import com.future.requestbean.DiseaseUpdate;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.tx.Tx;

@ControllerMapping("/disease")
public class DiseaseController extends BaseController {
	@Inject
	private DiseaseDao diseaseDao;

	@Before(Tx.class)
	public void addDisease() {
		DiseaseAdd diseaseAdd = getJsonBody(DiseaseAdd.class);
		Disease disease = beanToModel(diseaseAdd, Disease.class);
		this.diseaseDao.save(disease);
		renderSuccess();
	}

	@Before(Tx.class)
	public void deleteDisease(@NotNull(message = "请选择删除的对象") Long diseaseId) {
		this.diseaseDao.deleteById(diseaseId);
		renderSuccess();
	}

	public void queryDisease() {
		renderResult("diseases", this.diseaseDao.findAll());
	}

	@Before(Tx.class)
	public void diseaseUpadate() {
		Disease disease = getJsonBodyToModel(DiseaseUpdate.class, Disease.class);
		this.diseaseDao.update(disease);
		renderSuccess();
	}
}
